let app = getApp();
let location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    storeGoodsList: [],
    searchStoreList: [],
    storeName:'',
    longitude: '',
    latitude: '',
    goodsPage:1,
    goodsLastPage:0,
    goodsMore:false,
    storePage:1,
    storeLastPage:0,
    storeMore:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.keyword??'') !== '') {
      this.setData({
        storeName: options.keyword
      })
      options.keyword.trim();
      let arr = wx.getStorageSync('searchHistory') || [];
      let searchArr = [];
      if(arr.length > 0 ) {
        arr = JSON.parse(arr);
        let len = arr.length;
        console.log(len);
        if (len >= 10) {
          arr.pop();
        }
      }
      let timer = Date.parse(new Date());
      arr.unshift({name: options.keyword, key: timer});
      searchArr = arr.filter((currentValue, currentIndex, selfArr) =>{ //去重
        return selfArr.findIndex(x =>x.name === currentValue.name) === currentIndex
      });
      wx.setStorageSync('searchHistory', JSON.stringify(searchArr));
      
      this.searchGoods();
    }
    try {
      wx.removeStorageSync('applyaddresspoint');
    } catch (e) {
      // Do something when catch error
    }
    this._getLocation((mapdata, e) => {
      console.log(e);
      this.setData({
        longitude: e.lng,
        latitude: e.lat,
      });
      this.searchStore();
      console.log(this.data.longitude);
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    try {
      let value = wx.getStorageSync('applyaddresspoint');
      console.log(value);
      if (value) {
        console.log(value);
        this.setData({
          longitude: e.lng,
          latitude: e.lat,
        });
        console.log(this.data.longitude);
        // Do something with return value
      }
    } catch (e) {
      console.log(e);
    }
  },
  storeInput( event) {
    console.log(event);
    this.setData({
      storeName: event.detail.value
    })
  },
  commercialOn(event) { //跳转商户
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageF/consumption/commodity/commodity?storeId=' + id
    })
  },
  goodsNavTo(event) { //跳转商品
    let id = event.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id
    })
  },
  searchClick() {
    if((this.data.storeName??'') !== '') {
      this.searchStore();
      this.searchGoods();
    }
  },
  searchStore() { //搜索商户
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.search.searchStore");
    let json = {
      store_name:this.data.storeName,
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      page: this.data.storePage
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (!this.data.storeLastPage) {
            this.data.storeLastPage = 0;
          }
          this.setData({
            searchStoreList:res.data.data,
            storeMore:true
          })
        } else {
          this.data.storePage = this.data.storePage - 1;
          this.goodsMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  searchGoods() { //搜索商品
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.search.searchGoods");
    let json = {
      search: {
        keyword: this.data.storeName
      },
      page: this.data.goodsPage,
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (!this.data.goodsLastPage) {
            this.data.goodsLastPage = 0;
          }
          this.setData({
            storeGoodsList:res.data.data,
            goodsMore: true,
            goodsLastPage: res.data.last_page
          })
        } else {
          // this.data.goodsPage = this.data.goodsPage - 1;
          this.goodsMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goodsMoreData() {
    this.data.goodsMore = false; // 防止多次请求分页数据
    if (this.data.goodsPage >= this.data.goodsLastPage) {
      console.log('2343242');
      return;
    } else {
      this.data.goodsPage = this.data.goodsPage + 1;
      let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.search.searchGoods");
      let json = {
        search: {
          keyword: this.data.storeName
        },
        page: this.data.goodsPage,
        paginate: 5
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          this.data.goodsMore = true;
          if (res.result == 1) {
            this.setData({
              storeGoodsList : this.data.storeGoodsList.concat(list),
              goodsLastPage: res.data.last_page
            })
          } else {
            // this.data.goodsPage = this.data.goodsPage - 1;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
          console.log(this.data.storeGoodsList);
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  storeMoreData() {
    this.data.goodsMore = false; // 防止多次请求分页数据
    if (this.data.storePage >= this.data.storeLastPage) {
      return;
    } else {
      this.data.storePage = this.data.storePage + 1;
      let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.search.searchStore");
      let json = {
        search: {
          keyword: this.data.storeName
        },
        page: this.data.storePage,
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.storeMore = true;
            this.setData({
              searchStoreList : this.data.searchStoreList.concat(list),
              storeLastPage: res.data.last_page
            })
          } else {
            this.data.storePage = this.data.storePage - 1;
            this.storeMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})